package com.tkx.dxb.service.cms.controller.api;

import com.tkx.dxb.common.base.result.R;
import com.tkx.dxb.service.cms.entity.Ad;
import com.tkx.dxb.service.cms.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : victor
 * @since : 2020/12/7
 */
//@CrossOrigin
@Api(tags = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
public class ApiAdController {

    @Autowired
    private AdService adService;

    @ApiOperation("根据推荐位ID显示广告推荐")
    @GetMapping("list/{adTypeId}")
    public R listByAdTypeId(
            @ApiParam(value = "推荐ID",required = true)
            @PathVariable String adTypeId){
        List<Ad> ads =  adService.selectByAdTypeId(adTypeId);
        return R.ok().data("items",ads);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("save-test")
    public R saveAd(@RequestBody Ad ad){
        redisTemplate.opsForValue().set("ad",ad);
        return R.ok();
    }

    @GetMapping("get-test/{key}")
    public R getAd(@PathVariable String key){
        Ad  ad = (Ad)(redisTemplate.opsForValue().get(key));
        return R.ok().data("ad",ad);
    }
}
